<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <!--<el-form-item label="父id" prop="pid">
        <el-input
          v-model="queryParams.pid"
          placeholder="请输入父id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>-->
      <!--<el-form-item label="级数" prop="depth">
        <el-input
          v-model="queryParams.depth"
          placeholder="请输入级数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>-->
      <!--<el-form-item label="第三方接口字段名称" prop="thirdKey">
        <el-input
          v-model="queryParams.thirdKey"
          placeholder="请输入第三方接口字段名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>-->

      <el-form-item label="渠道号" prop="channelCode">
        <el-input
          v-model="queryParams.channelCode"
          placeholder="请输入渠道号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请求方向" prop="direction">
        <el-select v-model="queryParams.direction" placeholder="请选择请求方向" clearable>
          <el-option
            v-for="dict in direction"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="是否用于签名" prop="signFlag">
        <el-select v-model="queryParams.signFlag" placeholder="请选择是否用于签名" clearable>
          <el-option
            v-for="dict in sign_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="是否启用" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="请选择是否启用" clearable>
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['bus:platUrlColumnConfig:add']"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="platUrlColumnConfigList" @selection-change="handleSelectionChange">
        <el-table-column label="主键Id" align="center" prop="id" width="80" />
        <el-table-column label="父id" width="80" align="center" prop="pid" />
        <el-table-column label="级数" width="80" align="center" prop="depth" />
        <el-table-column label="第三方接口字段名称" min-width="150" align="center" prop="thirdKey" show-overflow-tooltip />
        <el-table-column label="第三方接口字段类型" width="150" align="center" prop="thirdKeyType" />
        <!-- <el-table-column label="接口字段描述" width="120" align="center" prop="keyDesc" /> -->
        <el-table-column label="平台实体字段名称" width="150" align="center" prop="entityKey" show-overflow-tooltip />
        <el-table-column label="平台实体类名称" min-width="150" align="center" prop="entityClazz" show-overflow-tooltip />
        <el-table-column label="时间类型格式化格式" width="150" align="center" prop="timeFormat" />
        <el-table-column label="渠道号" width="90" align="center" prop="channelCode" />
        <el-table-column label="请求方向" width="100" align="center" prop="direction">
          <template #default="scope">
            <dict-tag :options="direction" :value="scope.row.direction"/>
          </template>
        </el-table-column>
        <el-table-column label="是否用于签名" width="110" align="center" prop="signFlag">
          <template #default="scope">
            <dict-tag :options="sign_flag" :value="scope.row.signFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" align="center" prop="enabled" width="110">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.enabled"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bus:platUrlColumnConfig:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bus:platUrlColumnConfig:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接口地址字段映射对话框 -->
    <Dialog v-model="showDialog" :title="dialogTitle" width="60%">
      <edit-config 
        ref="editConfig"
        @cancel="cancel"
        @confirm="confirm"
      />
    </Dialog>
  </div>
</template>

<script setup name="PlatUrlColumnConfig">
import { listPlatUrlColumnConfig, getPlatUrlColumnConfig, delPlatUrlColumnConfig, addPlatUrlColumnConfig, updatePlatUrlColumnConfig } from "@/api/bus/platUrlColumnConfig"
import { nextTick, ref } from "vue"
import EditConfig from './edit.vue'

const editConfig = ref()
const { proxy } = getCurrentInstance()
const { direction, sign_flag, sys_normal_disable } = proxy.useDict('direction', 'sign_flag', 'sys_normal_disable')

const platUrlColumnConfigList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const showDialog = ref(false)
const dialogTitle = ref('')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    pid: null,
    depth: null,
    thirdKey: null,
    thirdKeyType: null,
    keyDesc: null,
    entityKey: null,
    entityClazz: null,
    timeFormat: null,
    channelCode: null,
    direction: null,
    signFlag: null,
    enabled: null
  }
})

const { queryParams } = toRefs(data)

/** 查询接口地址字段映射列表 */
function getList() {
  loading.value = true
  listPlatUrlColumnConfig(queryParams.value).then(response => {
    platUrlColumnConfigList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
}

const confirm = () => {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogTitle.value = '添加接口地址字段映射'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogTitle.value = '添加接口地址字段映射'
  nextTick(() => {
    editConfig.value.pageInit(_id)
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除接口地址字段映射编号为"' + _ids + '"的数据项？').then(function() {
    return delPlatUrlColumnConfig(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bus/platUrlColumnConfig/export', {
    ...queryParams.value
  }, `platUrlColumnConfig_${new Date().getTime()}.xlsx`)
}

getList()
</script>
